#include "E_Day.h"

//Default Constructor
E_Day::E_Day()
{
	this->e_meetings = new list<E_Meeting *>();
}
//Destructor
E_Day::~E_Day()
{
	if(!this->e_meetings->empty())
	{
		std::list<E_Meeting *>::iterator it;
		for ( it=this->e_meetings->begin() ; it != this->e_meetings->end(); it++ ){
			delete(*it); //TODO: how i del listOfAttendees
		}
	}
	delete (e_meetings);
}

//Copy Constructor
E_Day::E_Day(const E_Day& e_d):Day(e_d)
{
	this->e_meetings = e_d.e_meetings;
}

//Constructor for extended day
E_Day::E_Day(const int day):Day(day)
{
	this->e_meetings = new std::list<E_Meeting*>();
}
	
const bool E_Day::addMeeting(const E_Meeting &e_meeting)
{
		if(!this->findOverlap(e_meeting))
		{
			this->e_meetings->push_back(new E_Meeting(e_meeting));
			//delete(&e_meeting); //TODO: check this ?
			cout << "meeting added" << endl;
			return true;
		}
		cout << "can't add meeting, overlap exists" << endl;
		return false;
}

/* virtual function need to implement*/

//const bool E_Day::delMeeting(const Meeting &meeting){} TODO: Impl?

void E_Day::printMeetings() const
{
	if(!this->e_meetings->empty())
	{
		std::list<E_Meeting *>::iterator it;
		char c = 'A';
		for ( it=this->e_meetings->begin() ; it != this->e_meetings->end(); it++ ){
			cout << "*** Meeting: " << c++ << " ***" <<endl;
			(*it)->printMeeting();
		}
	} 
	else
	{
		cout << "No meetings today!" << endl;
	}
}

const bool E_Day::findOverlap( const E_Meeting &e_meeting) const
{
	
	std::list<E_Meeting *>::const_iterator it;
	for(it = this->e_meetings->begin() ; it != this->e_meetings->end() ; it++)
	{
		if(e_meeting.isOverLapped(**it))
			return true;
	}
	return false;
}

E_Meeting* E_Day::findMeeting(const float start_t) const
{
		std::list<E_Meeting *>::iterator it;
		E_Meeting* ret_meeting = NULL;
		for(it = this->e_meetings->begin() ; it != this->e_meetings->end() ; it++)
		{
			if((*it)->getStartHour() == start_t)
			{
				ret_meeting = *it;
				break;
			}
		}
		if(ret_meeting == NULL)
			cout << "Meeting not found" << endl;
			
		return ret_meeting;
}

const bool E_Day::delMeeting(const E_Meeting &e_meeting)
{
	E_Meeting* meet_to_del = findMeeting(e_meeting.getStartHour());
	if(meet_to_del == NULL)
	{
		cout << "can't delete this meeting" << endl;
		return false;

	} 
	this->e_meetings->remove(meet_to_del);
	cout << "meeting deleted" << endl;
	return true;
}